(function () {
    "use strict";

    var app = angular.module('app');

    app.controller('sys.menu', SysMenuCtrl);

    SysMenuCtrl.$inject = [
        '$rootScope',
        '$scope',
        '$win',
        '$dialog',
        '$toast',
        'Api',
        'SysMenuService'
    ];

    function SysMenuCtrl($rootScope, $scope, $win, $dialog, $toast, Api, SysMenuService) {
        $scope.options = {
            api       : Api.sys.menu.list,
            childIndex: 'submenu',
            column    : [{
                text : '菜单名',
                name : 'name',
                ratio: 3,
                tree : true
            }, {
                text: '图标',
                name: 'icon'
            }, {
                text    : '是否子节点',
                name    : 'isLeaf',
                renderer: function (data) {
                    if (data.isLeaf) {
                        return '是';
                    }
                    return '否';
                }
            }, {
                text: '链接',
                name: 'sref'
            }, {
                text : '操作列',
                items: [{
                    icon   : 'fa fa-plus',
                    tooltip: '添加下级菜单',
                    disable: function (row) {
                        // 该按钮是否禁用
                        return !!row.isLeaf;
                    },
                    handler: function (o, row) {
                        createMenu(row.ik);
                    }
                }, {
                    icon   : 'fa fa-pencil-square-o',
                    tooltip: '修改',
                    handler: function (o, row) {
                        updateMenu(row.ik);
                    }
                }, {
                    icon   : 'fa fa-remove',
                    tooltip: '删除',
                    handler: function (o, row) {
                        var msg = '确认删除?';
                        if (!row.upIk) {

                            msg = '确认删除当前及其下级菜单?';
                        }
                        $dialog.confirm(msg, function (confirm) {
                            confirm && remove(row.ik);
                        });
                    }
                }]
            }]
        };

        var remove = function (ik) {
            SysMenuService.remove(ik)
                .then(function () {
                    $toast.success('删除成功!');
                    $scope.sysMenuTree.reload();
                    // todo 更新右侧菜单视图
                });
        };

        var createMenu = function (upIk) {
            $scope.sysMenu = {};
            if (upIk) {
                $scope.sysMenu.upIk = upIk;
                $scope.sysMenu.isLeaf = 1;
            } else {
                $scope.sysMenu.isLeaf = 0;
            }
            showDialog();
        };

        var updateMenu = function (ik) {
            //A.Loader.show($modalBody);
            SysMenuService.get(ik)
                .then(function (data) {
                    //A.Loader.hide($modalBody);
                    $scope.sysMenu = data;
                    showDialog();
                })
        };

        var sysMenuDialog;
        var showDialog = function () {
            sysMenuDialog = $win.open({
                template: 'sysMenuDialog',
                scope   : $scope
            });
        };
        var refreshView = function (data) {
            $scope.sysMenuTree.setTreeData(data);
            $rootScope.updateMenu(data);
        };

        $scope.sysMenu = {};

        $scope.create = function () {
            createMenu();
        };

        $scope.submit = function () {


            SysMenuService.save($scope.sysMenu)
                .then(function (result) {
                    if (result.success) {
                        $toast.success('操作成功!');
                        sysMenuDialog.close();
                        refreshView(result.data);
                    } else {
                        $toast.error(result.msg || '操作失败!');
                    }
                });
        }
    }
})();